﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO.IsolatedStorage;

namespace SilverlightCountDownTimer.Utils
{
    /// <summary>
    /// Isolated storage manager. It wraps all methods connected with isolated storage.
    /// </summary>
    public class IsolatedStorageManager
    {
        IsolatedStorageSettings isolatedSettings = IsolatedStorageSettings.ApplicationSettings;

        /// <summary>
        /// Save event's date to isolated storage
        /// </summary>
        /// <param name="eventTime"></param>
        public void SaveDate(DateTime eventTime)
        {
            try
            {
                if (isolatedSettings.Contains("eventDate"))
                {
                    isolatedSettings["eventDate"] = eventTime;
                }
                else
                {
                    isolatedSettings.Add("eventDate", eventTime);
                }
            }
            catch (System.Collections.Generic.KeyNotFoundException)
            {
            }
        }

        /// <summary>
        /// Read event's date from isolated storage
        /// </summary>
        /// <returns></returns>
        public DateTime ReadDate()
        {
            try
            {
                return (DateTime)isolatedSettings["eventDate"];
            }
            catch (Exception) { return DateTime.Now; }
        }

        /// <summary>
        /// Save cache date and duration.
        /// </summary>
        /// <param name="_eventTimeCacheDuration"></param>
        public void SaveCacheDuration(int _eventTimeCacheDuration)
        {
            try
            {
                if (!isolatedSettings.Contains("eventTimeCacheDuration"))
                {
                    isolatedSettings.Add("eventTimeCacheDuration", _eventTimeCacheDuration);
                    isolatedSettings.Add("eventTimeCacheDate", DateTime.Now);
                }
                else
                {
                    if (!ContainsValidEventTime())
                    {
                        isolatedSettings["eventTimeCacheDuration"] = _eventTimeCacheDuration;
                        isolatedSettings["eventTimeCacheDate"] = DateTime.Now;
                    }
                }
            }
            catch (System.Collections.Generic.KeyNotFoundException) { }
        }

        /// <summary>
        /// Check if saved event's date is still consistent with cache
        /// </summary>
        /// <returns></returns>
        public bool ContainsValidEventTime()
        {
            try
            {
                DateTime saveTime = (DateTime)isolatedSettings["eventTimeCacheDate"];
                int daysNumber = (int)isolatedSettings["eventTimeCacheDuration"];
                TimeSpan diff = DateTime.Now - saveTime;
                if (Math.Abs(diff.Days) > daysNumber)
                    return false;
                return true;
            }
            catch (System.Collections.Generic.KeyNotFoundException)
            {
                return false;
            }
        }
    }
}
